<template>
  <div>
    <!-- 面包屑导航区 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>权限管理</el-breadcrumb-item>
      <el-breadcrumb-item>角色列表</el-breadcrumb-item>
    </el-breadcrumb>
    <!-- 卡片试图 -->
    <el-card>
      <!-- 添加角色按钮区域 -->
      <el-row>
        <el-col>
          <el-button type="primary">添加角色</el-button>
        </el-col>
      </el-row>
      <!-- 角色列表区域 -->
      <el-table :data="rolesList"
                border
                stripe>
        <el-table-column type="expand">
          <template slot-scope="scope">
            <el-row :class="['bdbottom',i1===0?'bdtop':'','vcenter']"
                    v-for="(item1,i1) in scope.row.children"
                    :key="item1.id">
              <!-- 渲染一级权限 -->
              <el-col :span="5">
                <el-tag closable
                        @close="deleteItem(scope.row,item1.id)">{{item1.authName}}</el-tag>
                <i class="el-icon-caret-right"></i>
              </el-col>
              <!-- 渲染二级和三级权限 -->
              <el-col :span="19">
                <!-- 通过for循环嵌套渲染二级权限 -->
                <el-row :class="[i2 === 0? '':'bdtop','vcenter']"
                        v-for="(item2,i2) in item1.children "
                        :key='item2.id'>
                  <el-col :span="6">
                    <el-tag type="success"
                            closable
                            @close="deleteItem(scope.row,item2.id)">{{item2.authName}}</el-tag>
                    <i class="el-icon-caret-right"></i>
                  </el-col>
                  <el-col :span="18">
                    <el-tag type="warning"
                            v-for="(item3,i3) in item2.children"
                            :key="item3.id"
                            closable
                            @close="deleteItem(scope.row,item3.id)">{{item3.authName}}</el-tag>
                  </el-col>
                </el-row>
              </el-col>
            </el-row>
          </template>
        </el-table-column>
        <!-- 索引列 -->
        <el-table-column type="index"
                         label="#"></el-table-column>
        <el-table-column label='角色名称'
                         prop='roleName'></el-table-column>
        <el-table-column label='角色描述'
                         prop='roleDesc'></el-table-column>
        <el-table-column label='操作'
                         width="300px">
          <template slot-scope="scope">
            <el-button type="primary"
                       size="mini"
                       icon="el-icon-edit">编辑</el-button>
            <el-button type="danger"
                       size="mini"
                       icon="el-icon-delete">删除</el-button>
            <el-button type="warning"
                       size="mini"
                       icon="el-icon-setting"
                       @click="showSetRightDialog()">分配权限</el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>

    <!-- 分类权限对话框 -->
    <el-dialog title="分配权限"
               :visible.sync="setRightDialogVisible"
               width="50%">
      <!-- 树形控件 -->
      <el-tree :data="rightsList"
               :props="treeProps"
               :default-checked-keys="defaultKeys"
               show-checkbox
               default-expand-all></el-tree>
      <span slot="footer"
            class="dialog-footer">
        <el-button @click="setRightDialogVisible = false">取 消</el-button>
        <el-button type="primary"
                   @click="setRightDialogVisible = false">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>
<script>
export default {
  data () {
    return {
      // 所有角色列表数据
      rolesList: [
        {
          id: 30,
          roleName: '主管',
          roleDesc: '技术负责人',
          children: [
            {
              id: 301,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }, {
                  id: 3002,
                  authName: '分类参数',
                  path: null,
                  children: [{
                    id: 30002,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }, {
              id: 302,
              authName: '订单管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }, {
                  id: 3002,
                  authName: 'hisjhs ',
                  path: null,
                  children: [{
                    id: 30002,
                    authName: '删除商品',
                    path: null
                  }, {
                    id: 30003,
                    authName: '获取分类',
                    path: null
                  }, {
                    id: 30004,
                    authName: '获取分类详情',
                    path: null
                  }]
                }
              ]
            }, {
              id: 303,
              authName: '权限管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }, {
              id: 304,
              authName: '用户管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }, {
          id: 40,
          roleName: '测试角色',
          roleDesc: '测试角色描述',
          children: [
            {
              id: 401,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }, {
          id: 50,
          roleName: '测试角色2',
          roleDesc: '测试描述2',
          children: [
            {
              id: 501,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }, {
          id: 60,
          roleName: '哈哈哈',
          roleDesc: '技术人',
          children: [
            {
              id: 601,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }, {
          id: 70,
          roleName: '哈哈试试',
          roleDesc: '那就是说',
          children: [
            {
              id: 701,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }, {
          id: 80,
          roleName: '阿米撒娇',
          roleDesc: '技术负考生责人',
          children: [
            {
              id: 801,
              authName: '商品管理',
              path: 'null',
              children: [
                {
                  id: 3001,
                  authName: '商品列表',
                  path: null,
                  children: [{
                    id: 30001,
                    authName: '添加商品',
                    path: null
                  }]
                }
              ]
            }
          ]
        }],
      // 控制分配权限对话框的显示与隐藏
      setRightDialogVisible: false,
      // 所有权限的数据
      rightsList: [{
        authName: '删除商品',
        children: [{
          authName: '只能删除一级商品',
          children: [{
            authName: '删除吧',
            id: 111,
            path: 'okDelete',
            pid: '0111'
          }],
          id: 11,
          path: 'deleteOne',
          pid: '011'
        }, {
          authName: '只能删除二级商品',
          children: [],
          id: 12,
          path: 'deleteTwo',
          pid: '012'
        }],
        id: 1,
        path: 'delete',
        pid: '01'
      }, {
        authName: '增加商品',
        children: [{
          authName: '只能增加一级商品',
          children: [{
            authName: '增加吧',
            id: 222,
            path: 'okAdd',
            pid: '0222'
          }],
          id: 22,
          path: 'addOne',
          pid: '022'
        }, {
          authName: '只能增加二级商品',
          children: [],
          id: 12,
          path: 'addTwo',
          pid: '022'
        }, {
          authName: '添加商品',
          children: [],
          id: 13,
          path: 'tianjia',
          pid: '023'
        }],
        id: 2,
        path: 'add',
        pid: '02'
      }],
      // 树形控件的属性绑定对象
      treeProps: {
        children: 'children',
        label: 'authName'
      },
      // 默认选中的树形节点
      defaultKeys: []
    }
  },
  created () {
    // this.getRolesList()
  },
  methods: {
    // 获取所有角色的列表
    async getRolesList () {
      const { data: res } = await this.$http.get('roles')
      if (res.meta.status !== 200) {
        this.$message.error('获取角色列表失败')
      }
      this.rolesList = res.data
    },
    // 根据Id删除对应的权限
    async deleteItem (role, rightId) {
      const confirmResult = await this.$confirm('此操作将永久删除该文件, 是否继续?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).catch(err => err)
      if (confirmResult !== 'confirm') {
        return this.$message.info('取消了删除')
      }
      this.$message.success('删除成功')
      // const { data: res } = await this.$http.delete(`roles/${role.id}/rights/${rightId}`)
      // if (res.meta.status !== 200) {
      //   return this.$message.error('删除权限失败')
      // }
      // 调用这个函数会将元素重新渲染，导致每次删除，展开项都会合住。每次删除之后返回的数据都是最新的权限数据
      // this.getRolesList()
      // role.children = res.data
    },
    // 展示分配权限的对话框
    async showSetRightDialog () {
      // 获取所有权限的数据
      // const { data: res } = await this.$http.get('rights/tree')
      // if (res.meta.status !== 200) {
      //   return this.$message.error('获取权限失败')
      // }
      // // 把获取到的权限数据保存到data中
      // this.rightsList = res.data
      // 递归获取三级权限数据
      // this.getLeafKeys(role, this.defaultKeys)
      this.setRightDialogVisible = true
      console.log(this.rightsList)
    },
    // 通过递归形式，获取角色下的所有三级权限id,并保存到defKeys
    getLeafKeys (node, arr) {
      if (!node.children) {
        return arr.push(node.id)
      }
      node.children.forEach(item => {
        this.getLeafKeys(item, arr)
      })
    }
  }

}
</script>
<style scoped>
.el-tag {
  margin: 7px;
}
.bdtop {
  border-top: 1px solid #eee;
}
.bdbottom {
  border-bottom: 1px solid #eee;
}
.vcenter {
  display: flex;
  align-items: center;
}
</style>
